﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using UniMana.Utils.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using UniMana.Utils.Configs;
using UniMana.Data;

namespace UniMana.Win.Training
{
    public partial class FrmViewSchedule_Department : DevExpress.XtraEditors.XtraForm
    {
        Cls_ConvertDataToScheduleDepartment cls_Convert = new Cls_ConvertDataToScheduleDepartment();

        private DBMain m_data = new DBMain();
        private DataTable m_tblTable = new DataTable();

        private int m_iCatalogue_Department = 0;
        private DateTime m_dStartTime;
        private DateTime m_dEndTime;

        public DevExpress.Utils.WaitDialogForm dlg;

        public FrmViewSchedule_Department()
        {
            InitializeComponent();
        }

        private void FrmViewSchedule_Department_Load(object sender, EventArgs e)
        {
            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");            
            lueCatalogue_Department.EditValue = 11;
            m_dStartTime = SysDateTime.Now - new TimeSpan(30, 0, 0, 0);
            m_dEndTime = SysDateTime.Now + new TimeSpan(30, 0, 0, 0);
            dteStart.EditValue = m_dStartTime;
            dteEnd.EditValue = m_dEndTime;
        }

        /// <summary>
        /// Hàm xử lý và hiển thị dữ liệu thời khóa biểu của khoa
        /// </summary>

        private void LoadScheduleDepartment()
        {
            dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
            try
            {
                m_tblTable = new DataTable();
                m_tblTable = (m_data.m_DCReadOnly.Training_Session_Subject_Class_Rooms.GetGetDataScheduleDepartment(m_data.m_DCReadOnly.Training_TrainingProgram_Details,
                    m_data.m_DCReadOnly.Training_TrainingPrograms,
                    m_data.m_DCReadOnly.Training_Subject_Classes,
                    m_data.m_DCReadOnly.Training_Sessions,
                    m_data.m_DCReadOnly.Training_Subjects,
                    m_data.m_DCReadOnly.Training_Class_Rooms,
                    m_data.m_DCReadOnly.Personnel_Personnels,
                    m_data.m_DCReadOnly.Training_WeekDays,
                    m_iCatalogue_Department,
                    m_dStartTime,
                    m_dEndTime)).ToDataTable();
                if (m_tblTable.Rows.Count != 0)
                {
                    DataView dview = m_tblTable.DefaultView;
                    dview.Sort = "RegistTime ASC";
                    //dview.Sort = "Training_SessionKey ASC";
                    m_tblTable = dview.ToTable();
                    gctView.DataSource = cls_Convert.Implementation(m_tblTable);
                    dlg.Close();
                }
                else
                {
                    XtraMessageBox.Show("Không có lịch cho khoa trong thời gian chọn");
                    gctView.DataSource = null;
                    dlg.Close();
                }
            }
            catch { dlg.Close(); }
        }
       
        private void lbcNow_MouseLeave(object sender, EventArgs e)
        {
            this.lbcNow.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Underline))));
        }

        private void lbcNow_MouseHover(object sender, EventArgs e)
        {
            this.lbcNow.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Italic))));
        }

        /// <summary>
        /// Chọn hôm nay
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void lbcNow_Click(object sender, EventArgs e)
        {
            dteStart.EditValue = dteEnd.EditValue = SysDateTime.Now;
        }

        /// <summary>
        /// Chọn ngày bắt đầu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void dteStart_EditValueChanged(object sender, EventArgs e)
        {
            m_dStartTime = (DateTime)dteStart.EditValue;
        }

        /// <summary>
        /// Chọn ngày kết thúc
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void dteEnd_EditValueChanged(object sender, EventArgs e)
        {
            m_dEndTime = (DateTime)dteEnd.EditValue;
        }

        private void grvView_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.Column.FieldName == "Ngày")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Ngày"]);
                if (category == "Ngày")
                    //e.Appearance.BackColor = Color.DeepSkyBlue;
                    e.Appearance.BackColor = Color.Silver;
                else
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Buổi")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Buổi"]);
                if (category == "Buổi")
                    e.Appearance.BackColor = Color.Silver;
                else
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 2")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 2"]);
                if (category == "Thứ 2")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 3")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 3"]);
                if (category == "Thứ 3")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 4")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 4"]);
                if (category == "Thứ 4")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 5")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 5"]);
                if (category == "Thứ 5")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 6")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 6"]);
                if (category == "Thứ 6")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 7")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 7"]);
                if (category == "Thứ 7")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Chủ nhật")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Chủ nhật"]);
                if (category == "Chủ nhật")
                    e.Appearance.BackColor = Color.Silver;
            }
        }

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            m_iCatalogue_Department = int.Parse(lueCatalogue_Department.EditValue.ToString());
        }

        private void xucBar1_btnAddNew_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadScheduleDepartment();
        }

        private void xucBar1_btnExport_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "*.xlsx | *.xls";
            saveFile.Title = "Lưu tập tin excel";
            if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                grvView.ExportToXlsx(saveFile.FileName);
            }  
        }

        private void xucBar1_btnPrint_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gclNgay.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclNgay.AppearanceHeader.Options.UseForeColor = true;
            this.gclBuoi.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclBuoi.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu2.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu2.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu3.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu3.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu4.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu4.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu5.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu5.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu6.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu6.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu7.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu7.AppearanceHeader.Options.UseForeColor = true;
            this.gclChuNhat.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclChuNhat.AppearanceHeader.Options.UseForeColor = true;

            string strTenlop = lueCatalogue_Department.Text.ToUpper();
            this.printableComponentLink1.ClearDocument();
            this.printableComponentLink1.RtfReportHeader = this.printableComponentLink1.RtfReportHeader.Replace("khoa", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(strTenlop));
            string ngaythang = "Tân An," + " ngày" + " " + DateTime.Now.Day + " " + "tháng" + " " + DateTime.Now.Month + " " + "năm" + " " + DateTime.Now.Year;
            string nguoidung = SysGlobalVar.g_sta_sysUserLogin.Properties.FullName;
            this.printableComponentLink1.RtfReportFooter = this.printableComponentLink1.RtfReportFooter.Replace("nguoilap", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(nguoidung));
            this.printableComponentLink1.RtfReportFooter = this.printableComponentLink1.RtfReportFooter.Replace("ngaythang", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(ngaythang));
            printableComponentLink1.ShowPreview();

            this.gclNgay.AppearanceHeader.Options.UseForeColor = false;
            this.gclBuoi.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu2.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu3.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu4.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu5.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu6.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu7.AppearanceHeader.Options.UseForeColor = false;
            this.gclChuNhat.AppearanceHeader.Options.UseForeColor = false;
        }
    }
}
